package com.chuanjiabao.wisdom.ui.activity

import android.content.Intent
import android.databinding.DataBindingUtil
import android.graphics.Paint
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import android.view.animation.DecelerateInterpolator
import com.chuanjiabao.wisdom.R
import com.chuanjiabao.wisdom.bean.WxLoginCallBackBean
import com.chuanjiabao.wisdom.databinding.ActivityLoginBinding
import com.chuanjiabao.wisdom.ui.ActivityStack
import com.chuanjiabao.wisdom.ui.BaseActivity
import com.chuanjiabao.wisdom.ui.activity.login.UserNumLoginActivity
import com.chuanjiabao.wisdom.util.SpUtils
import com.chuanjiabao.wisdom.util.ToastUtil
import com.chuanjiabao.wisdom.util.dialog.WxHintDialog
import com.chuanjiabao.wisdom.util.getPlatformInfo
import com.chuanjiabao.wisdom.util.setShareConfig
import com.lzy.okgo.utils.OkLogger
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.UMShareConfig
import com.umeng.socialize.bean.SHARE_MEDIA

class LoginActivity : BaseActivity() {

    private lateinit var binding: ActivityLoginBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login)
        binding.activity = this
        initView()
    }

    private fun initView() {
        val anim = AnimationUtils.loadAnimation(this, R.anim.secret_bottom_in)
        anim.interpolator = DecelerateInterpolator()
        binding.llModueBottom.startAnimation(anim)

        binding.loginUser.paint.apply {
            flags = Paint.UNDERLINE_TEXT_FLAG
            isAntiAlias = true
        }

        binding.loginDeal.paint.apply {
            flags = Paint.UNDERLINE_TEXT_FLAG
            isAntiAlias = true
        }
    }

    fun noPoint(view: View) {
        if (!binding.loginCheck.isChecked) {
            ToastUtil.showToast("请阅读并同意协议政策")
            return
        }

        setShareConfig(UMShareConfig().apply { isNeedAuthOnGetUserInfo = true })
        getPlatformInfo(SHARE_MEDIA.WEIXIN) {
            onComplete { _, _, data ->
                val bean = WxLoginCallBackBean().apply {
                    access_token = data["accessToken"] ?: ""
                    expires_in = data["expiration"] ?: "0"
                    openid = data["openid"]
                    refresh_token = data["RefreshToken"]
                    scope = "snsapi_userinfo"
                    unionid = data["uid"]
                }
                SpUtils.put("module_unionid", bean.unionid)
                SpUtils.putBean("module_wx_bean", bean)
                startActivity(Intent(this@LoginActivity, UserNumLoginActivity::class.java)
                        .putExtra("module_unionid", bean.unionid))
            }
            onError { _, _, throwable ->
                OkLogger.e(throwable.message)
                ToastUtil.showToast("授权失败")
            }
        }
    }

    fun okPoint(view: View) {
        if (!binding.loginCheck.isChecked) {
            ToastUtil.showToast("请阅读并同意协议政策")
            return
        }

        startActivity(Intent(this@LoginActivity, UserNumLoginActivity::class.java))
        ActivityStack.screenManager.popActivities(this::class.java)
    }

    fun userDeal(view: View) {
        WxHintDialog(this).apply {
            setContent("http://home.zzwisdom.com/deal.html")
            setTitleHint("用户协议")
            setBtnHint("确定")
            setOneBtn()
            show()
        }
    }

    fun secretDeal(view: View) {
        WxHintDialog(this).apply {
            setContent("https://home.zzwisdom.com/private.html")
            setTitleHint("隐私政策")
            setBtnHint("确定")
            setOneBtn()
            show()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this@LoginActivity).onActivityResult(requestCode, resultCode, data)
    }

}
